package com.world.rickcloudendpoint.logic.net;

import com.world.rickcloudendpoint.message.AbstractNetMessage;
import com.world.rickcloudendpoint.service.net.process.ITcpMessageProcessor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

/**
 * @description: 直接消息处理业务逻辑
 * @author: jlsong
 * @date: 2021/12/23 23:18:52
 * @version: 1.0
 */
@Log4j2
@Component
public class NetMessageTcpDispatchLogic {

    /**
     * 处理的消息总数
     */
    public long statisticsMessageCount = 0;


    /**
     * 派遣分发直接处理， 针对tcp协议
     * @param msg 消息
     * @param messageProcessor 处理器
     */
    public void dispatchTcpMessage(AbstractNetMessage msg, ITcpMessageProcessor messageProcessor) {
        if (msg == null) {
            log.warn("Message is null.");
            return;
        }
        long begin = System.nanoTime();
        statisticsMessageCount++;
        try {
//            NettyTcpSession clientSesion = (NettyTcpSession) msg.getAttribute(MessageAttributeEnum.DISPATCH_SESSION);
//            if(clientSesion != null){
//                GameServerConfigService gameServerConfigService = LocalMananger.getInstance().getLocalSpringServiceManager().getGameServerConfigService();
//                RpcConfig rpcConfig = gameServerConfigService.getRpcConfig();
//                if(rpcConfig.getSdRpcServiceProvider().isWorldOpen()){
//                    Loggers.serverStatusStatistics.debug("processor session" + clientSesion.getPlayerId() + " process message" + msg.getCmd());
//                    clientSesion.addNetMessage(msg);
//                }
//                clientSesion.addNetMessage(msg);
//            }else{
//                log.info("session is closed, the message is unDispatch");
//            }

        } catch (Exception e) {
            log.error("The message is unDispatch,the reason is {} ", e.getMessage(), e);

        } finally {
//            msg.removeAttribute(MessageAttributeEnum.DISPATCH_SESSION);

            // 特例，统计时间跨度
            long time = (System.nanoTime() - begin) / (1000 * 1000);
            if (time > 1) {
                log.info("Dispatch message id: {} Time: {} Total: {} ", msg.getCmd(), time, statisticsMessageCount);
            }
        }
    }
}
